import React from 'react';
import { withRouter } from 'react-router-dom';
import { connect } from 'react-redux';
import * as actions from '@pages/Home/store/action';
import { getRouterRequest } from '@router/store/action';

class CheckLogin extends React.Component {
    componentDidMount() {
        const obj = { method: "loginRequest", api: "/web/rest/user/isLogin", cmd: "islogin", request: {} };
        this.props._isLogin(obj);
        if (this.props.isGetMenu) {
            this.props.getMenu(this.props.history);
        }
        if (!this.props.isLogin) {
            this.props.history.push('/login');
        }
    }

    componentDidUpdate() {
        if (!this.props.isLogin) {
            this.props.history.push('/login');
        }
    }

    render() {
        return this.props.children;
    }


}
const mapDispatchToProps = dispatch => {
    return {
        _isLogin(obj) {
            dispatch(actions.isloginRequest(obj))
        },
        getMenu() {
            dispatch(getRouterRequest(true))
        }
    }
}
export default connect(null, mapDispatchToProps)(withRouter(CheckLogin));